<script setup>
import { ref } from 'vue';
import { Edit, Delete } from '@element-plus/icons-vue';
import {
  commodityCategoryListService,
  categoryDeleteService
} from '@/apis/commodity';
import { CommonElePlusTableEventHandler as TableEventHandler } from '@/utilities/tableeventhandle';
import CommodityCategoryEdit from './components/CommodityCategoryEdit.vue';

const loading = ref(false);
const params = ref({
  categoryName: '',
  pageIndex: 1,
  pageSize: 10,
  sortField: '',
  isAsc: false
});

const categoryList = ref([]);
const total = ref(0);
const getCategoryList = async () => {
  loading.value = true;
  let res = await commodityCategoryListService(params.value);
  categoryList.value = res.data;
  total.value = res.total;

  loading.value = false;
};

getCategoryList();

const tableEventHanler = new TableEventHandler(params, getCategoryList);

const onSortChange = tableEventHanler.onSortChange;
const onSizeChange = tableEventHanler.onSizeChange;
const onCurrentChange = tableEventHanler.onCurrentChange;
const onSearch = tableEventHanler.onSearch;
const onReset = tableEventHanler.onResetFunc(() => {
  params.value.categoryName = '';
});

let categoryEditRef = ref();
const onEditCategory = (row) => {
  categoryEditRef.value.open(row);
};

// 删除分类
const confirmDialogVisible = ref(false);
const toDeleteItem = ref({});
const onDeleteCategory = (row) => {
  toDeleteItem.value = row;
  confirmDialogVisible.value = true;
};
const doDeleteCategory = async () => {
  let res = await categoryDeleteService({ id: toDeleteItem.value.id });
  ElMessage.success(res.msg);
  confirmDialogVisible.value = false;
  getCategoryList();
};

const onFinishEdit = onReset;
</script>

<template>
  <page-container title="分类管理">
    <template #extra>
      <el-button @click="onEditCategory({})">添加分类</el-button>
    </template>

    <!--表单区域-->
    <el-form inline>
      <el-form-item label="分类名称">
        <el-input v-model="params.categoryName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="categoryList"
      @sort-change="onSortChange"
      v-loading="loading"
    >
      <el-table-column prop="categoryName" label="分类名称"></el-table-column>
      <el-table-column
        prop="sortNum"
        label="排序号"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        sortable="custom"
      ></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditCategory(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDeleteCategory(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      v-model:current-page="params.pageIndex"
      v-model:page-size="params.pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
    />
  </page-container>

  <commodity-category-edit
    ref="categoryEditRef"
    @finishEdit="onFinishEdit"
  ></commodity-category-edit>

  <el-dialog v-model="confirmDialogVisible" title="提示" width="30%">
    <span class="dialog-span">
      是否确定删除分类：
      <strong>{{ toDeleteItem && toDeleteItem.categoryName }}</strong>
    </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="confirmDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="doDeleteCategory()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.pagination {
  margin-top: 20px;
  justify-content: flex-end;
}

.dialog-span {
  font-size: 16px;
}
</style>
